<?php
/**
 * 用户
 * 
 * Created	: 2010-10-11
 * Modified : 2010-10-15
 * @author  : 陈立强
 */
class User 
{
	
	public static $login_no_pass = true;
	
	public static $skillItemCfgNameList = array(
		1 => 'crop',
		2 => 'fruit',
		3 => 'animal',
		4 => 'gather',
	);
	
	public static function init($uid)
	{
		Farm::init($uid);
		FruitForest::init($uid);
	}
	
	/**
	 * 创建用户
	 * @param array $data
	 */
	public static function create($username)
	{
		global $DB, $L, $G;
	
		$now = time();
		//确认成功 新建用户并登录
		$userData = array(
			'username'  		=> $username,
			'email'  			=> $email,
			'state' 			=> 0,
			'reg_time'			=> $_SERVER['REQUEST_TIME'],
		);
		
		$rs = $DB->insert('user', $userData);
		
		if($rs){
			self::$login_no_pass = true;
			User::login($username);
		}
			
		return array(
			'val' => true
		);
	}
	/**
	 * 创建角色
	 * @param string $nickname
	 * @param int $sex
	 * @param string $city
	 */
	public static function createRole($nickname, $sex, $city)
	{
		global $DB, $L, $G;
		if (empty($_SESSION['uid'])) {
			return array(
				'val' => false,
				'msg' => $L['not_login']
			);
		}
		if (!empty($_SESSION['created_role'])) {
			return array(
				'val' => false,
				'msg' => $L['user_role_had_created']
			);
		}
		
		$today = strtotime('today');
		
		$uid = $_SESSION['uid'];
	
		//昵称格式错误
		if( preg_match ("/\.|\s|&|'|\"/i", $nickname)||is_numeric($nickname)||strlen($nickname)>18 ) {
			return array(
				'val' => false,
				'msg' => $L['nickname_error']
			);
		}
		$fitrAry = C('filter');
		$fitrAry[] = 'GM';
		$fitrAry[] = 'gm';
		
		// 非法,含有敏感词
		if (isIncFileterKw($nickname)) {
			return array(
				'val' => false,
				'msg' => $L['nickname_invalid']
			);
		}
		
		$DB->beginTransaction();
		
		
		$rs = self::readByNickname($nickname);
		
		if ($rs) {
			return array(
				'val' => false,
				'msg' => $L['nickname_is_exist']
			);
		} else {
			C('making/staff');
			C('build/shop');
			C('item/gift');
			C('item/treasure_box');
			C('common/interactive');
			$now = time();
			//确认认成功 新建用户并登录
			$userData = array(
				'nickname'  		=> $nickname,
				'city'  			=> $city,
				'sex' 				=> $sex,
				'state' 			=> 1,
				'updated_time' 		=> $now,
				'coin'				=> $G['init_user_coin'],
				'ticket'			=> $G['init_user_ticket'],
				'exchange_coupon'	=> $G['init_user_exchange_coupon'],
				'lvl'				=> $G['init_mc_lvl'],
				'exp'				=> 0,
				'add_depot_capacity_times'=> 0,
				'depot_max_capacity'=> $G['depot_init_capacity'],
				'bag_max_grid'		=> $G['bag_max_grid'],
				'farm_lvl'			=> 0,
				'fruit_forest_lvl'	=> 0,
				'raise_lvl'			=> 0,
				'gather_lvl'		=> 0,
			
				'farm_extend_times'			=> 0,
				'fruit_forest_extend_times'	=> 0,
				'raise_extend_times'		=> 0,
				'gather_extend_times'		=> 0,
				'reg_time'					=> $now,
				'making_recipe'				=> rand(1, $G['making_recipe_num']),
				'remain_pk_times'			=> $G['pk_times_per_day'],
				'remain_get_coin_times'		=> $G['help_get_coin_times_per_day'],
				'last_get_coin_time'		=> 0,
				'remain_clear_shop_times'	=> $G['help_friend_clean_rubbish_max_times'],
				'last_clear_shop_time'		=> 0,
				'remain_lure_guest_times'	=> $G['lure_guest_max_num'],
				'last_lure_guest_time'		=> 0,
				'present_times'				=> $G['present_gift_times_perday'],
				'last_present_time'			=> 0,
				'share_box_times'			=> $G['max_share_box_times'],
				'last_share_box_time'		=> 0,
				'be_shared_box_times'		=> $G['max_be_shared_box_times'],
				'last_be_shared_box_time'	=> 0,
				'seriate_steal_times'		=> 0,
				'last_steal_time'			=> 0,
				'energy_point'				=> $G['energy_default_value'],
				'energy_update_time'		=> $today,
			);
			$ret = $DB->update('user', $userData, 'uid='.$uid);
			
			$DB->setTransactionState(false);
			
			if($ret){
				$ret = Farm::init($uid);
				if ($ret) {
					$ret = FruitForest::init($uid);
				}
				if ($ret) {
					$ret = Raise::init($uid);
				}
				if ($ret) {
					$ret = Gather::init($uid);
				}
				if ($ret) {
					$ret = self::initUserProficiency($uid);
				}
				if ($ret) {
					$ret = Shop::create($uid);
				}
				if ($ret) {
					$_SESSION['nickname'] = $nickname;
					$_SESSION['created_role'] = true;
					$_SESSION['online_time'] = time();
				}
			}
			$DB->setTransactionState(true);
		}
		
		if ($ret) {
			$DB->commit();
			return array(
				'val' => true,
			);
		} else {
			$DB->rollBack();
			return array(
				'val' => false,
				'msg' => $L['create_role_failure']
			);
		}
	}
	
	/**
	 * 初始化用户熟练度的数据
	 * @param $uid
	 */
	public static function initUserProficiency($uid)
	{
		global $DB;
		
		$DB->beginTransaction();
		
		$cropList = C('item/crop');
		foreach ($cropList as $item_id => $cfg) {
			if ($cfg['skill_type'] == 'null') {
				continue;
			}
			$lvl = $cfg['lvl']==1 ? 1 : 0;
			$data = array(
				'uid'		=> $uid,
				'class'		=> 1,
				'type'		=> $cfg['skill_type'],
				'item_id'	=> $item_id,
				'lvl'		=> $lvl,
				'exp'		=> 0,
			);
			$DB->insert('user_proficiency', $data);
		}
		
		$cropList = C('item/fruit');
		foreach ($cropList as $item_id => $cfg) {
			$lvl = $cfg['lvl']==1 ? 1 : 0;
			$data = array(
				'uid'		=> $uid,
				'class'		=> 2,
				'type'		=> $cfg['skill_type'],
				'item_id'	=> $item_id,
				'lvl'		=> $lvl,
				'exp'		=> 0,
			);
			$DB->insert('user_proficiency', $data);
		}
		
		$cropList = C('item/animal');
		foreach ($cropList as $item_id => $cfg) {
			$lvl = $cfg['lvl']==1 ? 1 : 0;
			$data = array(
				'uid'		=> $uid,
				'class'		=> 3,
				'type'		=> $cfg['skill_type'],
				'item_id'	=> $item_id,
				'lvl'		=> $lvl,
				'exp'		=> 0,
			);
			$DB->insert('user_proficiency', $data);
		}
		
		$cropList = C('item/gather');
		foreach ($cropList as $item_id => $cfg) {
			$lvl = $cfg['lvl']==1 ? 1 : 0;
			$data = array(
				'uid'		=> $uid,
				'class'		=> 4,
				'type'		=> $cfg['skill_type'],
				'item_id'	=> $item_id,
				'lvl'		=> $lvl,
				'exp'		=> 0,
			);
			$DB->insert('user_proficiency', $data);
		}
		
		$DB->commit();
		
		return true;
	}
	
	/**
	 * 获取所有用户列表
	 * @param int $uid
	 */
	public static function getUserList($uid, $pageNo=1) 
	{
		global $DB, $L, $G;
		$sql = 'SELECT uid,nickname,lvl FROM user WHERE state=1 and uid<>'.$uid;
		
		$pageSize = $G[''];
	}
	
	/**
	 * 获取角色熟练度列表
	 * @param int $uid
	 */
	public static function getProficiencyList($uid)
	{
		global $DB, $L;
		
		$sql = 'SELECT * FROM user_proficiency WHERE uid='.$uid.' ORDER BY class asc,type asc,item_id asc';
		$tmp = $DB->getList($sql);
		
		$list = array();
		
		$itemList = C('item/item_list');
		$skillTypeNameList = C('item/skill_type_name');
		
		
		foreach ($tmp as $v) {
			if (!isset($list[$v['class']])) {
				$list[$v['class']] = array();
			}
			$itemCfg = $itemList[$v['item_id']];
			$record = array(
				'exp'		=> $v['exp'],
				'item_lvl'	=> $itemList[$v['item_id']]['lvl'],
				'lvl'		=> $v['lvl'],
				'item_id'	=> $v['item_id'],
				'item_type'	=> $itemCfg['type'],
				'title'		=> $itemCfg['title'],
				'type'		=> $skillTypeNameList[$v['type']]
			);
//			if ($itemCfg['type'] == 5) {
//				$record['item_type'] = 7;
//				$record['item_id'] = $itemCfg['product_id'];
//			}
			if (isset($list[$v['class']][$v['type']])) {
				$list[$v['class']][$v['type']][$v['item_id']] = $record;
			} else {
				$list[$v['class']][$v['type']] = array($v['item_id']=>$record);
			}
		}
		
		return array(
			'val'	=> true,
			'data'	=> $list
		);
	}
	
	/**
	 * 用户是否已创建角色
	 * @param  $uid
	 */
	public static function isCreatedRole()
	{
		global $DB, $L;
		
		return !empty($_SESSION['created_role']);
	}
	
	/**
	 * 用户是否存在
	 * @param string $username
	 */
	public static function isUserExists($username)
	{
		global $DB, $L;
		
		$sql = 'SELECT uid FROM user WHERE username=:username';
		$param = array(
			'username' => $username
		);
		$uid = $DB->getValue($sql, $param);
		
		return (bool)$uid;
	}
	
	/**
	 * 用户登录
	 * @param string $username
	 */
	public static function login($username)
	{
		global $DB, $L;
		
		$where    = "username=:username";
		$params = array(
			'username' => $username,
		);
		$sql  = 'SELECT * FROM user WHERE '.$where;
		$user = $DB->getValue($sql, $params);
		if (!$user) {
			return false;
		}
		
		$now = time();
		$ip = (float)sprintf('%u',ip2long($_SERVER['REMOTE_ADDR']));
		$data = array(
			'last_login' => $now,
			'last_login_ip' => $ip,
			'last_online' => $now,
			'is_online' => 1,
		);
		$where = 'uid='.$user['uid'];
		$ret = $DB->update('user', $data, $where);
		
		if ($ret) {
			$_SESSION['uid']		= $user['uid'];
			$_SESSION['nickname']	= $user['nickname'];
			$_SESSION['username']	= $user['username'];
			$_SESSION['sessionid']	= session_id();
			global $m;
			$m->set('sessionid_'.$_SESSION['uid'], session_id());
			if ($user['state'] == 1) {
				$_SESSION['created_role']	= true;
			} else {
				$_SESSION['created_role']	= false;
			}
			self::refresh($user['uid']);
			$_SESSION['online_time'] = $now;
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 更新用户在线时间
	 * @param int $uid
	 */
	public static function updateOnline($uid) 
	{
		global $DB, $L, $G;
		$now = time();
		$_SESSION['online_time'] = $now;
		$sql = 'UPDATE user SET last_online='.$now.' WHERE uid='.$uid;
		$ret = $DB->exeSql($sql);
		
		return (boolean)$ret;
	}
	
	/**
	 * 刷新用户数据
	 * @param int $uid
	 */
	public static function refresh($uid)
	{
		global $DB, $L, $G;
		
		$today = strtotime('today');
		C('making/staff');
		C('build/shop');
		
		$sql = 'SELECT * FROM user WHERE uid='.(int)$uid;
		$user = $DB->getValue($sql);
		
		$updateData = array();
		// 更新PK次数和时间
		if ($user['last_pk_time'] < $today) {
			$updateData['last_pk_time'] = $today;
			$updateData['remain_pk_times'] = $G['pk_times_per_day'];
		}
		// 每日剩余可帮好友收取金钱次数
		if ($user['last_get_coin_time'] < $today) {
			$updateData['last_pk_time'] = $today;
			$updateData['remain_get_coin_times'] = $G['help_get_coin_times_per_day'];
		}
		// 每日剩余可帮好友收取金钱次数
		if ($user['last_clear_shop_time'] < $today) {
			$updateData['last_clear_shop_time'] = $today;
			$updateData['remain_clear_shop_times'] = $G['help_friend_clean_rubbish_max_times'];
		}
		// 每日剩余可拉拢好友顾客次数
		if ($user['last_lure_guest_time'] < $today) {
			$updateData['last_lure_guest_time'] = $today;
			$updateData['remain_lure_guest_times'] = $G['lure_guest_max_num'];
		}
		// 当天剩余赠送礼物次数
		if ($user['last_present_time'] < $today) {
			C('item/gift');
			$updateData['last_present_time'] = $today;
			$updateData['present_times'] = $G['present_gift_times_perday'];
		}
		// 当天剩余分享宝箱次数
		if ($user['last_share_box_time'] < $today) {
			C('item/treasure_box');
			$updateData['last_share_box_time'] = $today;
			$updateData['share_box_times'] = $G['max_share_box_times'];
		}
		// 当天剩余被分享宝箱次数
		if ($user['last_be_shared_box_time'] < $today) {
			if (!isset($G['max_be_shared_box_times'])) {
				C('item/treasure_box');
			}
			$updateData['last_be_shared_box_time'] = $today;
			$updateData['be_shared_box_times'] = $G['max_be_shared_box_times'];
		}
		if (count($updateData) > 0) {
			$ret = $DB->update('user', $updateData, 'uid='.$uid);
		}
		
		return true;
	}
	
	/**
	 * 注销登录状态
	 */
	public static function logout()
	{
		global $DB;
		$data = array(
			'last_online'	=> time(),
			'is_online'		=> 0,
		);
		$where = 'uid='.$_SESSION['uid'];
		$ret = $DB->update('user', $data, $where);
		$_SESSION = array();
		session_destroy();
		return true;
	}
	
	/**
	 * 搜索用户
	 * @param int $uid
	 * @param string $name
	 */
	public static function search($uid, $name)
	{
		global $DB, $L;
		
		$sql = 'SELECT * FROM user WHERE username=\''.addslashes($name).'\'';
		$record = $DB->getValue($sql);
		
		$record['last_login'] = date('Y-m-d H:i', $record['last_login']);
		$record['last_login_ip'] = long2ip($record['last_login_ip']);
		$record['last_online'] = date('Y-m-d H:i', $record['last_online']);
		
		return $record;
	}
	
	/**
	 * 屏蔽用户(好友也可屏蔽)
	 * @param int $uid 当前玩家ID
	 * @param int|string $touser 添加为黑名单的目标玩家ID或昵称
	 * @param array $options 屏蔽选项
	 * array(
	 * 		'ban_opinion'	=> 0,
	 * 		'ban_msg'		=> 1,
	 * 		'ban_chat'		=> 1
	 * )
	 */
	public static function filter($uid, $touser, $options)
	{
		global $DB, $L;
		
		if (is_int_string($touser)) {
			$touid = (int)$touser;
		} else {
			$touid = self::readByNickname($touser, 'uid');
		}
		if (!$touid) {
			return array(
				'val' => false,
				'msg' => $L['user_not_exists']
			);
		}
		
		$where = 'uid='.$uid.' and to_uid='.(int)$touid;
		$sql = 'SELECT * FROM user_filter WHERE '.$where;
		$user = $DB->getValue($sql);
		if ($user) {
			$update = false;
			$data = array();
			foreach ($options as $field=>$val) {
				if ($user[$field] != $val) {
					$update = true;
					$data[$field] = $val;
				}
			}
			if ($update) {
				$ret = $DB->update('user_filter', $data, $where);
			} else {
				$ret = true;
			}
		} else {
			$data = array(
				'uid'			=> $uid,
				'to_uid'		=> $touid,
				'ban_invite'	=> 1,// 必须
				'ban_opinion'	=> isset($options['ban_opinion']) ? (int)$options['ban_opinion'] : 1,
				'ban_msg'		=> isset($options['ban_msg']) ? (int)$options['ban_msg'] : 1,
				'ban_chat'		=> isset($options['ban_chat']) ? (int)$options['ban_chat'] : 1,
			);
			$ret = $DB->insert('user_filter', $data);
		}
		
		$result['val'] = (boolean)$ret;
		if ($ret) {
			$result['data'] = array(
				'to_uid' => $touid
			);
		} else {
			$result['msg'] = $L['Failure'];
			$str = var_export($DB->errors, true);
			// 记录错误日志
			Log::save('friend_filter', $str);
		}

		return $result;
		
	}
	
	/**
	 * 获取黑名单列表
	 * @param int $uid
	 */
	public static function getFileterList($uid)
	{
		global $DB, $L, $G;
		
		// 取好友ID列表
		$sql = 'SELECT * FROM user_filter WHERE uid='.$uid;
		$list = $DB->getList($sql);
		if (!$list) {
			return array();
		}
		
		$touidStr = '';
		foreach ($list as $value) {
			$touidStr .= $value['to_uid'].',';
		}
		$touidStr = substr($touidStr, 0, -1);
		
		$sql = 'SELECT uid,nickname FROM user WHERE uid IN ('.$touidStr.')';
		$userList = $DB->getList($sql);
		$userList = array2simple($userList, 'uid', 'nickname');
		
		foreach ($list as $key => $value) {
			if (isset($userList[$value['to_uid']])) {
				$list[$key]['nickname'] = $userList[$value['to_uid']];
			} else {
				$list[$key]['nickname'] = '';
			}
		}
		
		return $list;
	}
	
	/**
	 * 根据用户ID获取昵称
	 * @param int $uid
	 */
	public static function getNickname($uid)
	{
		global $DB;
		$sql  = 'SELECT nickname FROM user WHERE uid='.(int)$uid;
		$nickname = $DB->getValue($sql);
		return $nickname;
	}
	
	/**
	 * 读取用户信息
	 * @param int $uid
	 */
	public static function read($uid, $field='*', $moreInfo=false)
	{
		global $DB, $G;
		$sql  = 'SELECT '.$field.' FROM user WHERE uid='.(int)$uid;
		$user = $DB->getValue($sql);
		
		if ($field == '*') {
			if (is_array($user)) {
				$user['is_vip'] = self::isVip($uid);
				$user['is_gm'] = self::isGm($uid);
			}
			$expList = C('role/exp');
			$user['lvlup_exp'] = $expList[$user['lvl']];// 当前等级满经验
		}
		
		if ($field == '*' && $moreInfo) {
			// 店铺信息
			$user['shop'] = Shop::getInfo($uid);
			C('build/making');
			$user['making'] = array();
			foreach ($G['make_class_type_list'] as $classId) {
				$user['making'][$classId] = Making::getInfo($uid, $classId);
			}
		}
		
		
		return $user;
	}
	
	/**
	 * 根据昵称读取用户信息
	 * @param int $nickname
	 */
	public static function readByNickname($nickname, $field='*')
	{
		global $DB;
		$sql  = 'SELECT '.$field.' FROM user WHERE nickname=:nickname';
		$param = array('nickname' => $nickname);
		$user = $DB->getValue($sql, $param);
		
		return $user;
	}
	
	/**
	 * 根据用户名读取用户信息
	 * @param int $username
	 */
	public static function readByUsername($username, $field='*')
	{
		global $DB;
		$sql  = 'SELECT '.$field.' FROM user WHERE username=:username';
		$param = array('username' => $username);
		$user = $DB->getValue($sql, $param);
		
		return $user;
	}
	
	/**
	 * 更改用户指字段值 
	 * @param int $value
	 * @param string $field
	 * @param boolean $newValue
	 */
	public static function changeValue($uid, $value, $field, $newValue=1)
	{
		global $DB;
		if ($newValue) {
			$sql = 'UPDATE user SET '.$field.'='.(int)$value.' where uid='.(int)$uid;
		} else {
			$sql = 'UPDATE user SET '.$field.'='.$field.'+'.(int)$value.' where uid='.(int)$uid;
		}
		$DB->exeSql($sql);
	}
	
	/**
	 * 判断用户是否VIP玩家
	 * @param int $uid 用户ID
	 */
	public static function isVip($uid)
	{
		return true;
	}
	
	/**
	 * 判断用户是否GM
	 * @param int $uid
	 */
	public static function isGM($uid)
	{
		return true;
		if ($uid == 1) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 玩家添加经验/自动升级
	 * @param int $uid 玩家ID
	 * @param int $exp 增加的经验值
	 */
	public static function addExp($uid, $exp)
	{
		global $DB, $L;
		$sql    = 'SELECT lvl,exp FROM user WHERE uid='.(int)$uid;
		$user   = $DB->getValue($sql);
		$newExp = $user['exp'] + $exp;
		$lvl    = $user['lvl'];
		
		$expList = C('role/exp');
		end($expList);
		$maxLvl = key($expList);// 玩家可以升级的最高等级
		$maxExp = $expList[$lvl];// 当前等级满经验
		
		// 满级且经验超过最高经验值,将经验设为最高经验
		if ($user['lvl'] == $maxLvl && $newExp > $maxExp) {
			$newExp = $maxExp;
		} else if ($newExp >= $maxExp) { // 需要升级
			$newExp -= $maxExp;
			$lvl++;
			for ($i=$lvl; $i<=$maxLvl; $i++) {
				// 剩余经验不足以再升级
				if ($newExp < $expList[$lvl]) {
					break;
				} else {// 继续升级
					$newExp -= $expList[$lvl];
					$lvl++;
				}
			}
		} else {
			
		}
//		if ($type == '') {
//			$expField = 'exp';
//			$lvlField = 'lvl';
//		} else {
//			$expField = $type.'_exp';
//			$lvlField = $type.'_lvl';
//		}
		$data = array(
			'exp'		=> $newExp,
			'lvl'		=> $lvl,
		);
		$ret = $DB->update('user', $data, 'uid='.$uid);
		$result['val'] = $ret;
		if ($ret) {
			$result['data'] = $data;
		} else {
			$result['msg'] = $L['Failure'];
			$data = var_export($DB->errors, true);
			// 记录错误日志
			Log::save('user_add_exp', $data);
		}
		return $result;
	}
	
	/**
	 * 增加熟练度
	 * @param int $uid
	 * @param int $exp 要增加的熟练度
	 * @param int $item_id 熟练度类型,即物品ID
	 * @param int $class 熟练度分类,即1-农田,2-果林,3-养殖,4-采集
	 */
	public static function addProficiency($uid, $exp, $item_id, $class)
	{
		global $DB, $L, $G;
		if ($item_id == 100) {
			return array('val' => true);
		}
		$sql    = 'SELECT lvl,exp FROM user_proficiency WHERE uid='.(int)$uid.' and item_id=:item_id';
		$param  = array('item_id' => $item_id);
		$user   = $DB->getValue($sql, $param);
		if (!$user) {
			$insert = true;
			$user = array(
				'lvl' => 1,
				'exp' => 0
			);
		} else {
			$insert = false;
		}
		$newExp = $user['exp'] + $exp;
		$lvl    = $user['lvl'];
		
		$cfgList = C('item/'.self::$skillItemCfgNameList[$class]);
		$cfgInfo = $cfgList[$item_id];
		$expList = C('role/proficiency_exp');
		end($expList);
		$maxLvl = key($expList);// 玩家可以升级的最高等级
		if ($lvl > $maxLvl) {
			$lvl = $maxLvl;
		}
		$maxExp = $expList[$lvl][$cfgInfo['lvl']];
			
		// 满级且熟练度超过最高熟练度,将熟练度设为最高熟练度
		if ($lvl == $maxLvl && $newExp > $maxExp) {
			$newExp = 0;
			$lvl++;
		} else if ($newExp >= $maxExp) { // 需要升级
			$newExp -= $maxExp;
			$lvl++;
			for ($i=$lvl; $i<=$maxLvl; $i++) {
				// 剩余熟练度不足以再升级
				if ($newExp < $expList[$lvl][$cfgInfo['lvl']]) {
					break;
				} else {// 继续升级
					$newExp -= $expList[$lvl][$cfgInfo['lvl']];
					$lvl++;
				}
			}
		} else {
			
		}
		
		if ($lvl > $maxLvl) {
			$lvl = $maxLvl;
		}
		
		
		if ($insert) {
			$data = array(
				'uid'		=> $uid,
				'class'		=> $class,
				'type'		=> $cfgList[$item_id]['skill_type'],
				'item_id'	=> $item_id,
				'lvl'		=> $lvl,
				'exp'		=> $newExp,
			);
			$ret = $DB->insert('user_proficiency', $data);
		} else {
			$data = array(
				'exp' => $newExp,
				'lvl' => $lvl,
				'item_id' => $item_id
			);
			$ret = $DB->update('user_proficiency', $data, 'uid='.$uid.' AND item_id=:item_id');
			if (false !== $ret) {
				$ret = true;
			}
		}
		
		// 达到开启下一个熟练度的要求,同时开启下一个熟练度
		if ($ret && $lvl == $G['pre_proficiency_lvl'] && $cfgInfo['next_item_id']) {
			$data = array(
				'exp' => 0,
				'lvl' => 1,
				'item_id' => $cfgInfo['next_item_id']
			);
			$ret = $DB->update('user_proficiency', $data, 'uid='.$uid.' AND item_id=:item_id');
		}
		
		$result['val'] = (boolean)$ret;
		if ($ret) {
			$DB->commit();
			$result['data'] = $data;
		} else {
			$DB->rollBack();
			$result['msg'] = $L['Failure'];
			$data = var_export($DB->errors, true);
			// 记录错误日志
			Log::save('user_add_proficiency', $data);
		}
		return $result;
	}
	
	/**
	 * 获取用户是否在线
	 * @param int|string $user
	 * @return boolean
	 */
	public static function isOnline($user)
	{
		global $DB, $G;
		if (is_int_string($user)) {
			$sql = 'SELECT last_online FROM user WHERE uid='.$user.' AND is_online=1';
			$param = array('uid' => $user);
		} else {
			$sql = 'SELECT last_online FROM user WHERE nickname=:nickname AND is_online=1';
			$param = array('nickname' => $user);
		}
		$last_online = $DB->getValue($sql, $param);
		
		if ($last_online && (time() - $last_online) < $G['session_time'] ) {
			return true;
		} else {
			return false;
		}
	}
	
	public static function test()
	{
		echo 333;
		return array(
			'cookie'  => var_export($_COOKIE, true),
			'session' => var_export($_SESSION,true),
		);
	}
	
}